package main.java.com.lee.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/2/11
 */
public class FairAndNotFairLockTest {
    public static void main(String[] args) {

        ReentrantLock notFairLock = new ReentrantLock();
        MyCLHLock fairLock = new MyCLHLock();

        IntStream.rangeClosed(1, 9).forEach(i->{
            Thread thread = new Thread(String.valueOf(i)){
                @Override
                public void run() {
                    notFairLock(notFairLock);
//                    fairLock(fairLock);
                }
            };
            thread.start();
        });
    }

    public static void notFairLock(Lock lock) {
        lock.lock();
        System.out.println("线程: " + Thread.currentThread().getName() + ": 加锁成功");
        lock.unlock();
    }

    public static void fairLock(MyCLHLock lock) {
        lock.lock();
        System.out.println("线程: " + Thread.currentThread().getName() + ": 加锁成功");
        lock.unlock();
    }
}
